const app = getApp()
const {
  throttle
} = require('/util/public')
Component({
  mixins: [],
  data: {

  },
  props: {
    onLgin: (data) => data,
    show: true
  },
  didMount() {},
  didUpdate() {},
  didUnmount() {},
  methods: {
    closemodalOpened() {
      let obj = {
        loginShow: false,
        user_header: '',
        user_name: '',
        phone: '',
        login: false
      }
      this.props.onLgin(obj);
    },
    onSubmit: function (e) {
      my.showLoading({
        content: '登录中,请等待..',
      });
      console.log("6666+", e)
      my.getOpenUserInfo({
        fail: (res) => {
          my.alert({
            content: "请先登录后使用"
          });
        },
        success: (res) => {
          console.log("getOpenUserInfo=================>>>>>>", res);
          this.login(res.response)
            .then((res) => {
              // console.log("11",res)
              let obj = {
                loginShow: false,
                user_header: res.data.user.header,
                user_name: res.data.user.uname,
                ipone: res.data.user.phone,
                login: true
              }

              this.props.onLgin(obj);
            })
        }
      });

    },

    // // 登录
    login(response, func) {
      //console.log("======================login登录=========================")
      return new Promise((resolve, reject) => {
        const that = this;
        func = func ? func : () => {};
        response = response ? response : "";
        if (!response) {
          return
        }
        my.getAuthCode({
          scopes: 'auth_base',
          success: (res) => {
            const appIdRes = my.getAppIdSync();

            let deviceInfor = my.getSystemInfoSync()
            console.log("getAuthCode===========>>>", res)
            let sbInfor = {}
            sbInfor.currentBattery = deviceInfor.currentBattery
            sbInfor.brand = deviceInfor.brand
            sbInfor.system = deviceInfor.system
            sbInfor.language = deviceInfor.language
            sbInfor.model = deviceInfor.model
            sbInfor.version = deviceInfor.version
            sbInfor.fontSizeSetting = deviceInfor.fontSizeSetting
            sbInfor.platform = deviceInfor.platform
            sbInfor.storage = deviceInfor.storage
            sbInfor.app = deviceInfor.app
            console.log("112233++++", deviceInfor)
            let data = {
              devInfo: JSON.stringify(sbInfor),
              authCode: res.authCode,
              appletId: appIdRes.appId ? appIdRes.appId : '2021004124696308',
              param: response,
              promotionAccount: my.getStorageSync({
                key: 'promotionAccount'
              }).data ? my.getStorageSync({
                key: 'promotionAccount'
              }).data.promotionAccount : null,
              flowState: my.getStorageSync({
                key: 'promotionAccount'
              }).data ? my.getStorageSync({
                key: 'promotionAccount'
              }).data.flowState : null
            }
            app.user.login(data)
              .then(json => {
                console.log("===============>>>登陆", json)
                my.hideLoading()
                if (json.code != 200) {
                  my.alert({
                    content: json.msg
                  });
                  return;
                }
                // json.data.user.id = 57692
                if (json.data) {
                  my.setStorage({
                    key: 'user',
                    data: json.data.user,
                  })
                  my.setStorage({
                    key: 'token',
                    data: json.data.token,
                  })
                  resolve(json)
                }
              })
          },
          fail: () => {
            reject()
          }
        });

      })

    },

  },
});